<template>
  <div class='m-classesChild'>
    <div class="title">
      上级分类：{{ $route.query.name || '' }}
    </div>
    <div style='padding: 0 0 20px;'>
      <el-button type='primary' @click='createClass'>创建子级分类</el-button>
    </div>
    <a-table :data='data' :cols='tableKeys' key='t1'>
      <el-table-column align="center" label="操作" width='240' slot='ahead'>
        <template slot-scope="scope">
          <div class='m-firmClass-table-handle'>
            <el-button size='mini' @click='updateClass(scope.row.id, "cancel")'>修改</el-button>
            <el-button size='mini' @click='deleteClass(scope.row.id)'>删除</el-button>
          </div>
        </template>
      </el-table-column>
    </a-table>
    <div class='u-pagination'>
      <!-- 分页 -->
      <pagination ref='page' @change='getData' :action='fetchData' :pageSize='20'></pagination>
    </div>
    <u-dialog ref='dialog' @close='closeDialog' :openType='openType' />
  </div>
</template>

<script>
import uDialog from '@/module/sys/attribute/child-two-dialog'
import { mapMutations } from 'vuex'

export default {
  name: 'mClassesChild',

  components: {
    uDialog
  },

  data() {
    return {
      tableKeys: [
        { prop: 'name', label: '分类名称', minWidth: '100' }
      ],
      data: [],
      ifRequesting: false,
      openType: 'add',
      id: '',
      attrId: ''
    }
  },

  mounted() {
    this.id = this.$route.query.id
    if (!this.id) {
      return this.$router.push({ name: 'main' })
    }
    this.$refs.page.changePage()
  },

  methods: {
    ...mapMutations(['setTabRoutes']),

    routerPush(type, args) {
      // push 到缓存里
      this.setTabRoutes({ type: 'add', obj: { ...this.getRouterInfoAccordingName(type), ...args } })
    },

    async fetchData(aPageNum, aPageSize) {
      return this.$api.utilsGetClass({
        aPageNum,
        aPageSize,
        aClassId: this.id
      })
    },

    getData(data) {
      this.data = data
    },

    createClass() {
      this.openType = 'add'
      this.$refs.dialog.__show()
    },

    deleteClass(aId) {
      if (this.ifRequesting) return false
      this.__messageBox({
        title: '你确认要删除该分类吗？',
        successCallback: async () => {
          this.ifRequesting = true
          const { code } = await this.$api.utilsDeleteClass({ aId })
          this.ifRequesting = false
          this.$refs.page.changePage()
          return code
        }
      })
    },

    updateClass(aId) {
      this.openType = 'modify'
      this.$refs.dialog.__show('modify', aId)
    },

    toChild(id) {
      this.routerPush('sysClasses', { query: { id } })
      this.$router.push({ name: 'sysClasses', query: { id } })
    },

    closeDialog(type) {
      if (type) {
        this.$refs.page.changePage()
      }
    }
  }
}
</script>

<style lang='less'>
.m-classesChild {
  & > .title {
    padding-bottom: 20px;
    margin-bottom: 20px;
    border-bottom: 1px solid #e5e5e5;
    color: rgb(147, 147, 147);
    font-size: 14px;
  }
}
</style>